package com.flyme.module.file.provider.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.file.client.entity.SysCataLog;
import com.flyme.module.file.provider.service.SysCataLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author flyme
 */
@Slf4j
@Api(tags = "课程目录管理")
@RestController
@RequestMapping("/catalog")
public class SysCataLogController extends BaseController<SysCataLogService, SysCataLog> {

    @ApiOperation(value = "获取课程目录")
    @PostMapping(value = "/selectTreeData")
    public ResultBody selectTreeData(@RequestParam(value = "courseId") Long courseId) {
        List list = bizService.selectCataLog(courseId);
        return ResultBody.success(list);
    }

    @ApiOperation(value = "获取课程一级目录")
    @PostMapping(value = "/selectFirstTreeData")
    public ResultBody selectFirstTreeData(@RequestParam(value = "courseId") Long courseId) {
        List list = bizService.selectOne(courseId);
        return ResultBody.success(list);
    }


    /**
     * 课程目录列表
     *
     * @return
     */
    @ApiOperation(value = "获取课程目录列表", notes = "获取课程目录列表")
    @PostMapping(value = "/list")
    public ResultBody list(@ModelAttribute(value = "pagerInfo") PagerInfo pagerInfo, SysCataLog cataLog) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("cl.id,cataLogTitle,cataLogNo,cataLogUrl,videoId,sortOrder,createTime");
        cq.eq("courseId", cataLog.getId());
        cq.eq("parentId",cataLog.getParentId());
        cq.orderByAsc("sortOrder");
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    @ApiOperation(value = "添加课程目录", notes = "添加课程目录")
    @PostMapping(value = "/add")
    public ResultBody addCataLog(@ModelAttribute(value = "cataLog") SysCataLog cataLog) {
        boolean result = bizService.addCataLog(cataLog);
        return ResultBody.success(result);
    }

    @ApiOperation(value = "编辑课程目录", notes = "编辑课程目录")
    @PostMapping(value = "/edit")
    public ResultBody edit(SysCataLog cataLog) {
        boolean result = bizService.updateCataLog(cataLog);
        return ResultBody.success(result);
    }

    @ApiOperation(value = "删除课程目录", notes = "删除课程目录")
    @PostMapping(value = "/delById/{cataLogId}")
    public ResultBody delById(@PathVariable Long cataLogId) {
        return bizService.delById(cataLogId);
    }

    /**
     * 更新课程目录
     *
     * @return
     */
    @ApiOperation(value = "更新课程目录", notes = "更新课程目录")
    @PostMapping(value = "/updateCataLogNo")
    public ResultBody updateCataLogNo(@RequestParam("id") Long id, @RequestParam("cataLogNo") String cataLogNo) {
        boolean result = bizService.update(new UpdateWrapper<SysCataLog>().set("cataLogNo", cataLogNo).eq("id", id));
        return ResultBody.ok("设置成功").setData(result);
    }


    /**
     * 更新排序号
     *
     * @return
     */
    @ApiOperation(value = "更新排序号", notes = "更新排序号")
    @PostMapping(value = "/updateSortOrder")
    public ResultBody updateSortOrder(@RequestParam("id") Long id, @RequestParam("sortOrder") String sortOrder) {
        boolean result = bizService.update(new UpdateWrapper<SysCataLog>().set("sortOrder", sortOrder).eq("id", id));
        return ResultBody.ok("设置成功").setData(result);
    }


    /**
     * 更新课程目录序号
     *
     * @return
     */
    @ApiOperation(value = "更新课程目录序号", notes = "更新课程目录序号")
    @PostMapping(value = "/updateCataLogSortOrder")
    public ResultBody updateCataLogSortOrder(@RequestParam("courseId") Long courseId,@RequestParam("parentId") Long parentId, Integer n1,Integer n2) {
        boolean result = bizService.updateSortNo(courseId,parentId,n1,n2);
        return ResultBody.ok("设置成功").setData(result);
    }
}
